package se.webbzon.boltzmann.editor.propertyframe;

import java.awt.GridLayout;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.oschi01.worldobject.WorldMovableObject;

public abstract class WorldMovableObjectPropertyFrame extends WorldStaticObjectPropertyFrame {
	
	// The input fields for this property frame
	private final JTextField 	xvInput, yvInput, zvInput,
								xaInput, yaInput, zaInput;
	
	/*============================================================
	Constructors
	============================================================*/

	public WorldMovableObjectPropertyFrame(EditorModel model, WorldMovableObject instance) {
		super(model, instance);
		
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
		
		// The velocity panel
		JPanel velocityPanel = new JPanel();
		velocityPanel.setBorder(BorderFactory.createTitledBorder("Velocity"));
		velocityPanel.setLayout(new GridLayout(2,3));
		velocityPanel.add(new JLabel("x:"));
		velocityPanel.add(new JLabel("y:"));
		velocityPanel.add(new JLabel("z:"));
		xvInput = new JTextField("" + instance.getXVelocity());
		yvInput = new JTextField("" + instance.getYVelocity());
		zvInput = new JTextField("" + instance.getZVelocity());
		velocityPanel.add(xvInput);
		velocityPanel.add(yvInput);
		velocityPanel.add(zvInput);
		panel.add(velocityPanel);
		
		// The velocity panel
		JPanel accelerationPanel = new JPanel();
		accelerationPanel.setBorder(BorderFactory.createTitledBorder("Acceleration"));
		accelerationPanel.setLayout(new GridLayout(2,3));
		accelerationPanel.add(new JLabel("x:"));
		accelerationPanel.add(new JLabel("y:"));
		accelerationPanel.add(new JLabel("z:"));
		xaInput = new JTextField("" + instance.getXAcceleration());
		yaInput = new JTextField("" + instance.getYAcceleration());
		zaInput = new JTextField("" + instance.getZAcceleration());
		accelerationPanel.add(xaInput);
		accelerationPanel.add(yaInput);
		accelerationPanel.add(zaInput);
		panel.add(accelerationPanel);
		
		addPanel("Motion", panel);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public boolean apply(Collection<String> errorMessages) {
		if (super.apply(errorMessages)) try {
			WorldMovableObject instance = (WorldMovableObject) getInstance();
			final double xv = Double.parseDouble(xvInput.getText());
			final double yv = Double.parseDouble(xvInput.getText());
			final double zv = Double.parseDouble(xvInput.getText());
			final double xa = Double.parseDouble(xvInput.getText());
			final double ya = Double.parseDouble(xvInput.getText());
			final double za = Double.parseDouble(xvInput.getText());
			instance.setVelocity(xv, yv, zv);
			instance.setVelocity(xa, ya, za);
			return true;
		} catch (NumberFormatException e) {
			errorMessages.add(e.getMessage());
			return false;
		} else
			return false;
	}

}
